Use Strict


The "use strict" directive was introduced in ECMAScript 5 and is used to enable strict mode in JavaScript. Strict mode helps you write cleaner code by catching common coding mistakes and "unsafe" actions.

Enabling Strict Mode

Strict mode can be enabled for an entire script or for individual functions:

Global Strict Mode

"use strict";
x = 3.14; // This will cause an error because x is not declared

Function-Level Strict Mode

function myFunction() {
    "use strict";
    y = 3.14; // This will cause an error because y is not declared
}

Benefits of Strict Mode

Examples

Here are some examples demonstrating the use of strict mode in JavaScript:

"use strict";
x = 3.14; // Error: x is not defined

function myFunction() {
    "use strict";
    y = 3.14; // Error: y is not defined
}

delete Object.prototype; // Error in strict mode

For more detailed information, you can check out resources like W3Schools[^1^][1] and MDN Web Docs[^2^][2].